package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Friend;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;


import java.util.List;


@DubboService
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加好友
    @Override
    public void addFriend(Long userId, Long friendId) {
        //1.将friendId设置到User的好友中
        Query query1 = Query.query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId));
        //1.1判断好友关系是否存在
        if (!mongoTemplate.exists(query1, Friend.class)) {
            //1.2当不存在好友关系,封装添加
            Friend friend1 = new Friend();
            friend1.setUserId(userId);
            friend1.setFriendId(friendId);
            friend1.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend1);
        }
        //2.将User设置到friendId的好友中
        Query query2 = Query.query(Criteria.where("userId").is(friendId)
                .and("friendId").is(userId));
        //2.1判断关系是否存在
        if (!mongoTemplate.exists(query2, Friend.class)) {
            //2.2如果不存在,添加
            Friend friend2 = new Friend();
            friend2.setUserId(friendId);
            friend2.setFriendId(userId);
            friend2.setCreated(System.currentTimeMillis());
            //3.添加
            mongoTemplate.save(friend2);
        }
    }

    //根据用户的id查询好友列表
    @Override
    public List findByUserId(Long userId, Integer page, Integer pagesize) {
        //根据用户的id查询好友列表
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria).skip((page - 1) * pagesize)
                //按照时间排序
                .limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Friend> list = mongoTemplate.find(query, Friend.class);
        return list;
    }

    //删除好友关系
    @Override
    public void deleteFriend(Long userId, Long friendId) {
        //1.将friendId从 User的好友中删除
        Query query1 = Query.query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId));
        //1.1判断好友关系是否存在,存在删除
        if (mongoTemplate.exists(query1, Friend.class)) {
            //1.2存在既删除
            mongoTemplate.remove(query1, Friend.class);
        }

        //2.//将User从friendId的好友中删除
        Query query2 = Query.query(Criteria.where("userId").is(friendId)
                .and("friendId").is(userId));
        //2.1判断是否存在关系
        if (mongoTemplate.exists(query2, Friend.class)) {
            //2.1存在删除
            mongoTemplate.remove(query2, Friend.class);
        }
    }

    /**
     * 查询是否是好友关系
     * @param userId
     * @param targetUserId
     * @return
     */
    @Override
    public Boolean findFriend(Long userId, Long targetUserId) {
        //判断是否是好友关系
        Query query=Query.query(Criteria.where("userId").is(userId)
        .and("friendId").is(targetUserId));
        boolean exists = mongoTemplate.exists(query, Friend.class);
        return exists;
    }
}
